﻿using System;
using System.Collections.Generic;
using System.Text;
using com.dz.entity;
using System.Data.SqlClient;
using System.Data;
using com.dz.common;
namespace com.dz.dataaccess
{
    public class BookCommendDao
    {
        /// <summary>
        /// 分页获取推荐列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <param name="commendType"></param>
        /// <param name="pageType"></param>
        /// <returns></returns>
        public DataSet GetCommendListByPage(int pageIndex, int pageSize,  out int totalCount, Consts.CommendType commendType,Consts.PageType pageType)
        {
            totalCount = 0;
            try
            {
                SqlParameter[] parameters = {
					new SqlParameter("@commendType", SqlDbType.Int,4),
					new SqlParameter("@pageIndex", SqlDbType.Int,4),
					new SqlParameter("@pageSize", SqlDbType.Int,4),
                    new SqlParameter("@outTotalCount",SqlDbType.Int,4),
                    new SqlParameter("@totalCount",SqlDbType.Int,4)
                };
                parameters[0].Value = (int)commendType;
                parameters[1].Value = pageIndex;
                parameters[2].Value = pageSize;
                parameters[3].Value = (int)pageType;
                parameters[4].Direction = ParameterDirection.Output;
                DataSet ds = DbHelperSQL.RunProcedure("proc_tbCommendBook_Page_Get", parameters, "ds");
                if (!int.TryParse(parameters[4].Value.ToString(), out totalCount))
                    totalCount = 0;
                return ds;
            }
            catch (Exception ex)
            {
                throw new DataException("分页获取推荐列表失败!", ex);
            }
        }

        /// <summary>
        /// 添加书籍推荐
        /// </summary>
        /// <param name="bookId"></param>
        /// <param name="commendType"></param>
        /// <returns></returns>
        public int AddBookCommend(long bookId, Consts.CommendType commendType)
        {
            try
            {
                int rowsAffected;
                SqlParameter[] parameters = {
					new SqlParameter("@bookId", SqlDbType.BigInt,8),
					new SqlParameter("@commendType", SqlDbType.Int,4),
					new SqlParameter("@result", SqlDbType.Int,4)
                };
                parameters[0].Value = bookId;
                parameters[1].Value = (int)commendType;
                parameters[2].Direction = ParameterDirection.Output;


                DbHelperSQL.RunProcedure("proc_tbCommend_Add", parameters, out rowsAffected);
                return (int)parameters[2].Value;
            }
            catch (Exception ex)
            {
                throw new DataException("添加书籍推荐失败", ex);
            }
        }

      /// <summary>
      /// 删除推荐书籍
      /// </summary>
      /// <param name="bookId"></param>
      /// <param name="commendType"></param>
      /// <returns></returns>
        public int DeleteBookCommend(long bookId, Consts.CommendType commendType)
        {
            try
            {
                int rowsAffected;
                SqlParameter[] parameters = {
					new SqlParameter("@bookId", SqlDbType.BigInt,8),
					new SqlParameter("@commendType", SqlDbType.Int,4),
					new SqlParameter("@result", SqlDbType.Int,4)
                };
                parameters[0].Value = bookId;
                parameters[1].Value = (int)commendType;
                parameters[2].Direction = ParameterDirection.Output;


                DbHelperSQL.RunProcedure("proc_tbCommendBook_Delete", parameters, out rowsAffected);
                return (int)parameters[2].Value;
            }
            catch (Exception ex)
            {
                throw new DataException("删除推荐书籍失败", ex);
            }
        }
    }
}
